<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function addStrings(num1, num2) {
        let i = num1.length - 1;
        let j = num2.length - 1;
        let carry = 0;
        let result = "";

        while (i >= 0 || j >= 0 || carry > 0) {
          let digit1 = i >= 0 ? parseInt(num1[i]) : 0; // 如果 num1 已经遍历完，则 digit1 为 0
          let digit2 = j >= 0 ? parseInt(num2[j]) : 0; // 如果 num2 已经遍历完，则 digit2 为 0

          let sum = digit1 + digit2 + carry;
          carry = Math.floor(sum / 10); // 计算进位
          result = (sum % 10).toString() + result; // 将当前位的结果添加到结果字符串的开头

          i--;
          j--;
        }

        return result;
      }

      console.log(addStrings("456", "789")); // 输出
    </script>
  </body>
</html>
